package com.hongsheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.hongsheng.pojo.Customer;
import com.hongsheng.pojo.Package;
import com.hongsheng.pojo.R;
import com.hongsheng.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * @description 客户控制层
 **/
@RestController
@CrossOrigin
@RequestMapping("/customer")
public class CustomerController {

    //注入客户service
    @Autowired
    private CustomerService customerService;
    /**
     * @description 客户列表分页查询,模糊查询
     **/
    @PostMapping ("{currentPage}/{size}")
    public R getCustomersPage(@PathVariable int currentPage,@PathVariable int size,@RequestBody Customer customer){

        IPage<Customer> page = customerService.getPage(currentPage,size,customer);
        //当前页大于总页数时出现bug
        if(currentPage>page.getPages()){
            //以总页数再次查询一次
            page=customerService.getPage((int) page.getPages(), size,customer);

        }
        return new R(true,page);
    }

    /**
     * @description 根据客户id查询包装信息
     **/
    @GetMapping("/{id}")
    public R selectCustomerById(@PathVariable Integer id){
        return new R(true,customerService.getById(id));
    }

    /**
     * @description 修改客户
     * 客户信息以json传参
     **/
    @PutMapping()
    public R update(@RequestBody Customer customer){//json接受参数
        return  new R(customerService.updateCustomer(customer));//根据包装id修改包装信息
    }


    /**
     * @description 添加客户
     * 客户信息以json传参
     **/
    @PostMapping()
    public R add(@RequestBody Customer customer){
        return new R(customerService.insertCustomer(customer));
    }



    /**
     * @description id删除客户
     **/
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Integer id){
        return new R(customerService.removeById(id));
    }

}
